package com.huilan.etl.controller;

import com.huilan.etl.entity.AirlineInfoConfig;
import com.huilan.etl.message.IMessageService;
import com.huilan.etl.service.AirlineInfoConfigService;
import com.huilan.etl.util.MethodEnum;
import com.huilan.etl.util.XmlUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.ServiceLoader;

/**
 * @author ghy
 * @date 2018-12-27 14:00
 **/
@Slf4j
@RestController
public class TestController {

    @Autowired
    AirlineInfoConfigService airlineInfoConfigService;

    @GetMapping("test")
    public String test() throws Exception {
        String xml1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<Msg>\n" +
                "    <Head>\n" +
                "        <Svc_ServiceCode>ADXP_NAOMS_O_BASE_ALIE</Svc_ServiceCode>\n" +
                "        <Svc_Version>1.0</Svc_Version>\n" +
                "        <Svc_Sender_Org>ADXP</Svc_Sender_Org>\n" +
                "        <Svc_Sender>NAOMS</Svc_Sender>\n" +
                "        <Svc_Receiver_Org></Svc_Receiver_Org>\n" +
                "        <Svc_Receiver></Svc_Receiver>\n" +
                "        <Svc_SerialNumber></Svc_SerialNumber>\n" +
                "        <Svc_SessionId>20171208140658867</Svc_SessionId>\n" +
                "        <Svc_SendTimeStamp>20171208140658867</Svc_SendTimeStamp>\n" +
                "    </Head>\n" +
                "    <Body>\n" +
                "        <Airline>\n" +
                "            <Id>16694</Id>\n" +
                "            <Iata>CA</Iata>\n" +
                "            <Icao>CCA</Icao>\n" +
                "            <Region>D</Region>\n" +
                "            <Cn>国航</Cn>\n" +
                "            <En>AIR CHINA</En>\n" +
                "            <CnFull>中国国际航空公司</CnFull>\n" +
                "            <EnFull>AIR CHINA</EnFull>\n" +
                "            <Union>星空联盟</Union>\n" +
                "        </Airline>   \n" +
                "    </Body>\n" +
                "</Msg>\n";



        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<Msg>\n" +
                "    <Head>\n" +
                "        <Svc_ServiceCode>ADXP_NAOMS_O_BASE_ALDL</Svc_ServiceCode>\n" +
                "        <Svc_Version>1.0</Svc_Version>\n" +
                "        <Svc_Sender_Org>ADXP</Svc_Sender_Org>\n" +
                "        <Svc_Sender>NAOMS</Svc_Sender>\n" +
                "        <Svc_Receiver_Org>ADXP</Svc_Receiver_Org>\n" +
                "        <Svc_Receiver>FIDS</Svc_Receiver>\n" +
                "        <Svc_SerialNumber></Svc_SerialNumber>\n" +
                "        <Svc_SessionId>20171208140658867</Svc_SessionId>\n" +
                "        <Svc_SendTimeStamp>20171208140658867</Svc_SendTimeStamp>\n" +
                "    </Head>\n" +
                "    <Body>\n" +
                "        <AirlineList>\n" +
                "            <Airline>\n" +
                "                <Id>1669211</Id>\n" +
                "                <Iata>CA</Iata>\n" +
                "                <Icao>CCA</Icao>\n" +
                "                <Region>D</Region>\n" +
                "                <Cn>国航</Cn>\n" +
                "                <En>AIR CHINA</En>\n" +
                "                <CnFull>中国国际航空公司</CnFull>\n" +
                "                <EnFull>AIR CHINA</EnFull>\n" +
                "                <Union>星空联盟</Union>\n" +
                "            </Airline>\n" +
                "            <Airline>\n" +
                "                <Id>1669311</Id>\n" +
                "                <Iata>CA</Iata>\n" +
                "                <Icao>CCA</Icao>\n" +
                "                <Region>D</Region>\n" +
                "                <Cn>国航</Cn>\n" +
                "                <En>AIR CHINA</En>\n" +
                "                <CnFull>中国国际航空公司</CnFull>\n" +
                "                <EnFull>AIR CHINA</EnFull>\n" +
                "                <Union>星空联盟</Union>\n" +
                "            </Airline>\n" +
                "        </AirlineList>\n" +
                "    </Body>\n" +
                "</Msg>\n";

        String svcServiceCode = XmlUtil.getSvcServiceCode(xml);

        switch (MethodEnum.getByValue(svcServiceCode)){
            case ADXP_NAOMS_O_BASE_ALIE:
                System.out.println(MethodEnum.ADXP_NAOMS_O_BASE_ALIE.getServiceName());
                AirlineInfoConfig airline = XmlUtil.getBodyBean(xml, AirlineInfoConfig.class, AirlineInfoConfig.CLASS_NAME);
                airlineInfoConfigService.save(airline);
                System.out.println(airline.toString());
                break;
            case ADXP_NAOMS_O_BASE_ALDL:
                System.out.println(MethodEnum.ADXP_NAOMS_O_BASE_ALDL.getServiceName());
                List<AirlineInfoConfig> airlineList = XmlUtil.getBodyBeanList(xml, AirlineInfoConfig.class, AirlineInfoConfig.CLASS_NAME);
                airlineInfoConfigService.saveBatch(airlineList);
                System.out.println(airlineList.toString());
                break;
            default:
                System.out.println();
        }

        return "success";
    }


    @GetMapping("ddd")
    public void ddd() throws Exception {
        String insert = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<Msg>\n" +
                "    <Head>\n" +
                "        <Svc_ServiceCode>ADXP_NAOMS_O_BASE_ALIE</Svc_ServiceCode>\n" +
                "        <Svc_Version>1.0</Svc_Version>\n" +
                "        <Svc_Sender_Org>ADXP</Svc_Sender_Org>\n" +
                "        <Svc_Sender>NAOMS</Svc_Sender>\n" +
                "        <Svc_Receiver_Org></Svc_Receiver_Org>\n" +
                "        <Svc_Receiver></Svc_Receiver>\n" +
                "        <Svc_SerialNumber></Svc_SerialNumber>\n" +
                "        <Svc_SessionId>20171208140658867</Svc_SessionId>\n" +
                "        <Svc_SendTimeStamp>20171208140658867</Svc_SendTimeStamp>\n" +
                "    </Head>\n" +
                "    <Body>\n" +
                "        <Airline>\n" +
                "            <Id>16694</Id>\n" +
                "            <Iata>CA</Iata>\n" +
                "            <Icao>CCA</Icao>\n" +
                "            <Region>D</Region>\n" +
                "            <Cn>国航</Cn>\n" +
                "            <En>AIR CHINA</En>\n" +
                "            <CnFull>中国国际航空公司</CnFull>\n" +
                "            <EnFull>AIR CHINA</EnFull>\n" +
                "            <Union>星空联盟</Union>\n" +
                "        </Airline>   \n" +
                "    </Body>\n" +
                "</Msg>\n";

        String update = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<Msg>\n" +
                "    <Head>\n" +
                "        <Svc_ServiceCode>ADXP_NAOMS_O_BASE_ALUE</Svc_ServiceCode>\n" +
                "        <Svc_Version>1.0</Svc_Version>\n" +
                "        <Svc_Sender_Org>ADXP</Svc_Sender_Org>\n" +
                "        <Svc_Sender>NAOMS</Svc_Sender>\n" +
                "        <Svc_Receiver_Org></Svc_Receiver_Org>\n" +
                "        <Svc_Receiver></Svc_Receiver>\n" +
                "        <Svc_SerialNumber></Svc_SerialNumber>\n" +
                "        <Svc_SessionId>20171208140658867</Svc_SessionId>\n" +
                "        <Svc_SendTimeStamp>20171208140658867</Svc_SendTimeStamp>\n" +
                "    </Head>\n" +
                "    <Body>\n" +
                "        <Airline>\n" +
                "            <Id>16692</Id>\n" +
                "            <Iata>CA</Iata>\n" +
                "            <Icao>CCA</Icao>\n" +
                "            <Region>D</Region>\n" +
                "            <Cn>国航</Cn>\n" +
                "            <En>AIR CHINA</En>\n" +
                "            <CnFull>中国国际航空公司</CnFull>\n" +
                "            <EnFull>AIR CHINA</EnFull>\n" +
                "            <Union>星空联盟</Union>\n" +
                "        </Airline>   \n" +
                "    </Body>\n" +
                "</Msg>\n";

        String svcServiceCode = XmlUtil.getSvcServiceCode(update);


        ServiceLoader<IMessageService> serviceLoader = ServiceLoader.load(IMessageService.class);
        for (IMessageService messageService : serviceLoader){
            if(messageService.containsKey(svcServiceCode)){
                messageService.excute(svcServiceCode,update);
                break;
            }
        }
    }
}
